package com.mcp.converter.domain.rule.conversion.function;

import com.mcp.converter.domain.rule.conversion.ConversionRule;
import com.mcp.converter.shared.enums.IncompatibilityType;
import com.mcp.converter.shared.enums.SQLElementType;
import com.mcp.converter.domain.converter.ConversionSuggestion;
import com.mcp.converter.shared.response.sql.SQLElement;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * XML函数 (XMLAGG, XMLELEMENT等) 转换建议规则
 * 提示用户手动审查和调整XML相关函数的用法
 * @author zengxiong
 */
public class XmlFunctionsToManualReviewRule implements ConversionRule {

    private static final String RULE_ID = "CV_FN021";
    private static final String RULE_NAME = "XML函数手动审查";
    private static final String RULE_DESCRIPTION = "提示用户手动审查和调整XMLAGG, XMLELEMENT等XML相关函数的用法，因为OceanBase的支持可能有限或存在差异。";

    private ConversionSuggestion conversionSuggestion;
    private static final Set<String> XML_FUNCTION_NAMES = new HashSet<>(Arrays.asList(
            "XMLAGG", "XMLELEMENT", "XMLFOREST", "XMLCONCAT", "XMLPARSE", "XMLSERIALIZE"
    ));

    public XmlFunctionsToManualReviewRule() {
        this.conversionSuggestion = new ConversionSuggestion();
        this.conversionSuggestion.setSuggestion(
                "OceanBase对XML相关函数 (如 XMLAGG, XMLELEMENT, XMLFOREST, XMLCONCAT, XMLPARSE, XMLSERIALIZE等) 的支持可能不完整或行为有差异。" +
                "请仔细评估在OceanBase中使用XML函数的必要性。验证目标OceanBase版本对所需XML函数的支持程度和具体行为。" +
                "如果支持有限或不兼容，强烈建议考虑在应用层面处理XML生成/聚合，寻找替代的数据表示方法（如JSON），或简化XML操作。"
        );
        this.conversionSuggestion.setExample("XMLAGG(...) -> (需要手动审查和调整，可能需要在应用层实现或使用其他方式)");
        this.conversionSuggestion.setAutoConvertible(false);
        this.conversionSuggestion.setComplexity(ConversionSuggestion.Complexity.COMPLEX);
    }

    @Override
    public String getId() {
        return RULE_ID;
    }

    @Override
    public String getName() {
        return RULE_NAME;
    }

    @Override
    public String getDescription() {
        return RULE_DESCRIPTION;
    }

    @Override
    public IncompatibilityType getIncompatibilityType() {
        return IncompatibilityType.FUNCTION;
    }

    @Override
    public ConversionSuggestion getConversionSuggestion() {
        return conversionSuggestion;
    }

    @Override
    public boolean matches(Object target) {
        if (target instanceof SQLElement) {
            SQLElement element = (SQLElement) target;
            if (element.getType() == SQLElementType.FUNCTION && element.getName() != null) {
                return XML_FUNCTION_NAMES.contains(element.getName().toUpperCase());
            }
        }
        return false;
    }

    @Override
    public String convert(SQLElement element, ConversionContext context) {
        String originalSqlFragment = element.getValue() != null ? element.getValue() : "XML_FUNCTION(...)";
        if (context != null) {
            context.addConversionDetail(originalSqlFragment, originalSqlFragment,
                "XML函数 (" + (element.getName() != null ? element.getName() : "") + ") 无法自动转换。请参考建议手动审查和调整。");
        }
        return originalSqlFragment;
    }
}
